/**
 * Created by yu_huan on 2018/3/6.
 */
define(function () {

    var self, requestArgs;

    function Wdbj() {
        self = this;
        //self.productData = [];
        self.mData = {
            orderSet: {},
            form: {
                MMRT007: '',
                MMRT009: '',
                MMRT010: '',
                MMRT011: ''
            }
        };
        self.searchData = {
            keyword: ''
        };
        requestArgs = app.pageParameters['wdbj_page'];
        self.init();
    }

    Wdbj.prototype.getData = function (goods) {
        app.ajax({
            xurl: "api/wx/wdbj/getPartListData",
            xdata: {
                page:goods.page+1,
                start: goods.start(),
                limit: goods.limit,
                keyword: self.searchData.keyword
            },
            cb: function (result) {
                if (result.ok) {
                    if (goods.page == 0) {
                        goods.data.removeAll();
                    }
                    var data = result.data;
                    for (var i = 0; i < data.data.length; i++) {
                        data.data[i].MMRH004 = 1;
                        goods.data.push(data.data[i]);
                    }

                    var refresh = goods.refresh;
                    if (goods.page == 0) {
                        refresh.endPulldownToRefresh();
                        if (result.data.data.length == 0) {
                            var msg = '无备件...';
                            app.showNoData(goods._tip, msg);
                            refresh.disablePullupToRefresh();
                        } else {
                            app.hideNoData(goods._tip);
                            refresh.refresh(true);
                        }
                    }
                    goods.page += 1;
                    refresh.endPullupToRefresh(result.data.data.length < goods.limit);
                }

            }
        });
    };

    Wdbj.prototype.init = function () {

        mui.ready(function () {

            Vue.component('wdbj-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array
                },
                methods: {
                    minus: function (val) {
                        if (val.MMRH004 > 1) {
                            val.MMRH004--;
                        }
                    },
                    plus: function (val) {
                        if (val.MMRS008 > val.MMRH004) {
                            val.MMRH004++;
                        }
                    }
                }
            });

            new Vue({
                el: doc.getElementById('searchs'),
                data: {
                    md: self.searchData
                },
                watch: {
                    'md.keyword': function () {
                        var goods = self.mData.orderSet['scroll'];
                        goods.page = 0;
                        self.getData(goods);
                    }
                }
            });

            var refreshData = function () {
                var goods = self.mData.orderSet['scroll'];
                if (!goods) {
                    goods = self.mData.orderSet['scroll'] = {
                        page: 0,
                        limit: 15,
                        data: [],
                        start: function () {
                            return this.page * this.limit;
                        }
                    };

                    new Vue({
                        el: doc.getElementById('scroll'),
                        data: {
                            items: goods.data
                        }
                    });

                    goods._tip = $('#scroll');
                    goods.refresh = mui('#scroll').pullRefresh({
                        down: {
                            callback: function () {
                                goods.page = 0;
                                self.getData(goods);
                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self.getData(goods);
                            }
                        }
                    });
                }
            };

            refreshData(0);

            app.taps.call(win, [
                {
                    o: 'btn_bjsq',
                    cb: function (e) {
                        var goods = self.mData.orderSet['scroll'],
                            checkArr = [];
                        for (var i = 0; i < goods.data.length; i++) {
                            if (goods.data[i].checked == true) {
                                goods.data[i].MMRH002 = '1';// 默认收费
                                checkArr.push(app.cloneJSON(goods.data[i]));
                            }
                        }
                        var msg = requestArgs.cb(checkArr);
                        console.log(JSON.stringify(checkArr));
                        console.log(msg);
                        if (msg == 1) {
                            mui.back();
                        } else {
                            app.alert({
                                msg: '配件：' + msg + ',已经添加过了'
                            });
                        }

                    }
                }
            ]);

        });
    };
    return new Wdbj();
});